﻿using System;
using System.Collections.Generic;
using System.Linq;
using Crossroad.Core.Data;
using Crossroad.Core.Exceptions;
using Crossroad.Core.Models;
using Crossroad.OAuth.Model;
using Crossroad.Utils.Extensions;
using SqlSugar;

namespace Crossroad.OAuth.Service.Impl
{
    /// <summary>
    /// oauth2.0访问令牌表
    /// </summary>
    public class AccessTokenService: Access<AccessToken>, IAccessTokenService
    {
        public AccessTokenService(
        )
        {
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="method"></param>
        /// <param name="loadRelevantData">是否加载关联数据</param>
        public Pagination<AccessToken> GetPaging(
            int pageIndex,
            int pageSize,
            Action<ISugarQueryable<AccessToken>> method,
			bool loadRelevantData = true
        )
        {
            var query = db.Queryable<AccessToken>();
            method(query);
            var totalCount = 0;
            var page = new Pagination<AccessToken>
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
            };
            page.Records = query.ToPageList(page.PageIndex, page.PageSize, ref totalCount);
            page.TotalCount = totalCount;
			if(loadRelevantData)
                Load(page.Records);

            return page;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="method"></param>
        /// <param name="loadRelevantData">是否加载关联数据</param>
        /// <returns></returns>
        public  List<AccessToken> GetList(
            Action<ISugarQueryable<AccessToken>> method,
			bool loadRelevantData = true
        )
        {
            var query = db.Queryable<AccessToken>();
            method(query);
            var list = query.ToList();
			if(loadRelevantData)
				Load(list);

            return list;
        }

        /// <summary>
        /// 查询详情
        /// </summary>
        /// <param name="method"></param>
        /// <param name="loadRelevantData">是否加载关联数据</param>
        /// <returns></returns>
        public AccessToken GetEntity(
            Action<ISugarQueryable<AccessToken>> method,
			bool loadRelevantData = true
		)
        {
            var query = db.Queryable<AccessToken>();
            method(query);
            var model = query.First();
            if (loadRelevantData)
                Load(model);

            return model;
        }

        /// <summary>
        /// 查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="loadRelevantData">是否加载关联数据</param>
        /// <returns></returns>
        public AccessToken GetEntity(dynamic id, bool loadRelevantData = true)
        {
            var model = db.Queryable<AccessToken>().InSingle(id);
            if (loadRelevantData)
                Load(model);

            return model;
        }

        /// <summary>
        /// 根据id列表查询maps
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public Dictionary<string, AccessToken> GetMaps(List<string> idList)
        {
            var maps = new Dictionary<string, AccessToken>();
            if (idList.Count < 1)
                return maps;

            var list = db.Queryable<AccessToken>()
                .In(f => f.accessToken, idList.Distinct().ToArray())
                .ToList();

            list.ForEach(m =>
            {
                maps.Add(m.accessToken, m);
            });

            return maps;
        }

        /// <summary>
        /// 新增返回详情
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public AccessToken CreateAt(AccessToken model)
        {
            model = db.Insertable(model).ExecuteReturnEntity();

            return GetEntity(model.accessToken);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
		public AccessToken UpdateAt(AccessToken model) 
		{
			var rows = db.Updateable(model).IgnoreColumns(true).ExecuteCommand();

			return GetEntity(model.accessToken);
		}

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="source"></param>
        public void DeleteAt(AccessToken source)
        {
             var rows = db.Deleteable<AccessToken>().In(source.accessToken).ExecuteCommand();
            if (rows < 1)
                throw new LogicException("删除失败！");
        }


		#region 加载关联数据

        public void Load(List<AccessToken> list)
        {
            if (list.IsNull())
                return;
        }

        public void Load(AccessToken model)
        {
            if (model.IsNull())
                return;
        }

        #endregion


    }
}
